package org.logan.sample.web.base.engine;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

public class ExceptionHandler implements HandlerExceptionResolver {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @ResponseBody
    public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse rep, Object obj, Exception e) {
        Exception exception = (Exception) e;
        logger.error("异常原因：{}", e.getMessage());
        // Result resul = new Result();
        // resul.setStatus(1);
        // resul.setErrorCode(2);
        ModelAndView modelView = new ModelAndView();
        MappingJackson2JsonView json = new MappingJackson2JsonView();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("statusCode", "500");
        map.put("msg", "500-内部异常");
        json.setAttributesMap(map);
        modelView.setView(json);
        // ClientMessage clientMessage = new ClientMessage();
        // clientMessage.setStatusCode(exception.getErrorCode());
        // clientMessage.setMessage(ErrorMessage.getClientMessage(exception.getErrorCode()));
        // ModelAndView modelView = new ModelAndView("error");
        // modelView.addObject("clientMessage", clientMessage);
        //
        return modelView;
    }
}
